package com.kly.user.task.v2;

import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.user.db.dao.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RemoveDataTask {

    private static Log logger = LogFactory.getLog(RemoveDataTask.class);

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    @Resource
    UserOnlineLogDao userOnlineLogDao;
    @Resource
    UserMsgDao userMsgDao;
    @Resource
    UserCommonLogDao userCommonLogDao;
    @Resource
    UserWlmLogDao userWlmLogDao;
    @Resource
    UserHomePageRecordDao userHomePageRecordDao;


    private String localIP = NetUtil.getLocalhostStr();

    /**
     * 数据清理
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void run() {

        if (!taskJobExecutor.contains(localIP)) {
            logger.info("=============NotTaskJobExecutor, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }
        long startTime = System.currentTimeMillis();
        try {
            userOnlineLogDao.removeData();
            logger.info("removeOnlineLogCostTime:{}", (System.currentTimeMillis() - startTime) / 100);
        } catch (Exception e) {
            logger.error("removeUserOnlineLogError", e);
        }

        try {
            startTime = System.currentTimeMillis();
            userMsgDao.backup();
            userMsgDao.removeMsg();
            logger.info("removeMsgCostTime:{}", (System.currentTimeMillis() - startTime) / 100);
        } catch (Exception e) {
            logger.error("removeUserMsgError", e);
        }

        try {
            startTime = System.currentTimeMillis();
            userCommonLogDao.removeData();
            logger.info("removeCommonLogCostTime:{}", (System.currentTimeMillis() - startTime) / 100);
        } catch (Exception e) {
            logger.error("removeCommonLogError", e);
        }

        try {
            startTime = System.currentTimeMillis();
            userWlmLogDao.removeData();
            logger.info("costTime:{}", (System.currentTimeMillis() - startTime) / 100);
        } catch (Exception e) {
            logger.error("removeWlmLogCostTime", e);
        }

        try {
            startTime = System.currentTimeMillis();
            userHomePageRecordDao.removeData();
            logger.info("removeUserHomePageRecordCostTime:{}", (System.currentTimeMillis() - startTime) / 100);
        } catch (Exception e) {
            logger.error("removeUserHomePageRecordError", e);
        }

    }
}
